package com.example.a79273.shandianban.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.config.NetconfigYouHuo;
import com.example.a79273.shandianban.model.YouHuoBean;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by lenovo on 2016/8/29.
 */
public class YouHuoRecyclerAdapter extends RecyclerView.Adapter<YouHuoRecyclerAdapter.MyViewHolder> implements View.OnClickListener {
    private List<YouHuoBean.DataBean.ListBean> mData;
    private Context mContext;
    private ImageOptions mOptions;
    private OnRecyclerViewItemClickListener mOnItemClickListener;
    public YouHuoRecyclerAdapter(List<YouHuoBean.DataBean.ListBean> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        mOptions = new ImageOptions.Builder()
                .setUseMemCache(true)
                .setLoadingDrawableId(R.mipmap.mr11)
                .setFailureDrawableId(R.mipmap.mr11)
                .build();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(Integer) v.getTag());
        }
    }

    //点击事件
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , int position);
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_youhuoson_item,null);
        MyViewHolder holder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        YouHuoBean.DataBean.ListBean listBean = mData.get(position);
        String url = listBean.getUrl();
        x.image().bind(holder.mImageView, NetconfigYouHuo.PIN_JIE+url,mOptions);
        holder.itemView.setTag(position);
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView mImageView;
        public MyViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.iv_youhuoson_item_pic);
        }
    }
    //暴露接口的方法
    public  void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
